package com.mainbo.androidframework.utils;

import android.content.Context;
import android.util.DisplayMetrics;

/**
 * @author wei
 *         <b>创建时间</b> 2015/9/26
 *         需要修改，还需要加上屏幕dpi的判断
 */
public class DimenUtils {

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static int px2dip(Context context, float pxValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * displayMetrics.DENSITY_XXHIGH/DisplayMetrics.DENSITY_DEFAULT + 0.5f);
    }

    public static int px2sp(Context context, float pxValue) {

        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }
}
